<style lang="less" scoped="">

</style>
<template>
  <div>
    <pheader style="border-bottom: 1px solid #dcdae2; ">
      <div slot="left">{{applyName}}</div>
      <div slot="right" style="float:right;margin-right: 25px;">
        <Button v-if="params.serviceType === 'accountNoBigdata'" size="small" style="width: 60px;height: 25px;" @click="isPrint = true">打印</Button>
        <Button v-if="params.isEdit  != 1 && showSave" size="small" style="width: 60px;height: 25px;" @click="saveDraft()">保存</button>
        <Button type="primary" size="small" style="width: 60px;height: 25px;" @click="showSubmintPage()">提交</Button>
      </div>
    </pheader>
    <div style="position: relative;">
      <div style="overflow-y: auto;height:calc(100vh - 144px);background-color:#FFFFFF;">
        <component ref="apply" v-model="isPrint" :is="applyPage" :isEdit="params.isEdit" :draftsId="draftsId"
          :resourceInfo="params.resourceInfo" :children="params.reqId" :filesList="filesList" :childrenType="childrenType"
          :serviceType="params.serviceType" :params="params"></component>
      </div>
      <Spin size="large" fix v-if="loadingSpin"></Spin>
    </div>
    <Modal v-model="showSubmint" :mask-closable="false" :closable="false">
      <div style="position: relative;">
        <Form :model="submintInfo" :label-width="140" label-position="left" class="form" style="padding-top: 20px;height:200px">
          <template v-if="demandUsers.length == 0">
            <span style="text-align: center;padding-top: 20px">无需选择下一步处理人，请直接提交！</span>
          </template>
          <Row v-else>
            <Col span="20">
            <FormItem label="下一步处理人员：">
              <Select v-model="submintInfo.nextOperator.id" filterable clearable @keydown.native.enter.prevent
                @on-change="setNextOperator">
                <Option v-for="(item, i) in demandUsers" :value="item.id" :key="i" :label="item.user_name">
                  <span>{{item.user_name + (item.isleader == 1 ? "(领导)":'')}}</span>
                  <li v-for="i in item.structs " :key="i.id" style="margin-left: 10px;font-size:10px; color:#c5c8ce;">{{i.gName + (i.isleader == 1 ? "(领导)":'')}}</li>
                </Option>
              </Select>
            </FormItem>
            </Col>
          </Row>
        </Form>
        <Spin size="large" fix v-if="loadingModal"></Spin>
      </div>
      <div slot="footer">
        <Button v-if="!submintLoading" @click="showSubmint = false;submintLoading = false">取消</Button>
        <Button type="primary" :loading="submintLoading" @click="submint()">提交</Button>
      </div>
    </Modal>
    <notice v-show="showmd" :serviceType="serviceType"></notice>
  </div>
</template>
<script>
  import ajax from "../ajax_util"
  import vuex from 'vuex';
  import mcontainer from '../comp_chatter/mcontainer'
  import mheader from "../comp_chatter/mheader"
  import mpage from "../comp_chatter/mpage"
  import minfo from "../comp_chatter/minfo"

  import entry from './applyVue/apply_settledIn'
  import net from './applyVue/apply_network'
  import netmap from './applyVue/apply_netmapping'
  import pheader from "../comp_chatter/pheader"
  import children from './applyVue/apply_children'
  import account from './applyVue/apply_account'
  import resourceAllo from './applyVue/apply_children_resources'
  import severMachine from './applyVue/apply_server'
  import accountApply from './applyVue/apply_number'
  import severPlantform from './applyVue/apply_server_platform'
  import securityCheck from './applyVue/apply_safe'
  import sonAccountAllo from './applyVue/apply_children_account'

  import notice from '../base/notice'
  import loadBalance from './applyVue/apply_loadbalancing'
  import hightAuthoritys from './applyVue/apply_sys'
  import accountCancel from './applyVue/apply_accountCancel'
  import register from '../comp_user/registered2'
  import vip from './applyVue/apply_vip'
  import addGold from './applyVue/apply_vault'
  import applysuggest from './applyVue/apply_suggest'
  import shrinkage from './applyVue/apply_shrink'
  import noRealAccount from './applyVue/apply_noRealAccount'
  import entry_v2 from './applyVue/apply_settledIn_v2.vue'
  // import entry_v2 from './applyVue/apply_settledIn02.vue'


  export default {
    //申请基础页面
    name: "applyPage",
    components: {
      mheader,
      mpage,
      mcontainer,
      minfo,
      pheader,
      entry,
      net,
      children,
      account,
      resourceAllo,
      accountApply,
      severMachine,
      severPlantform,
      securityCheck,
      sonAccountAllo,
      notice,
      loadBalance,
      hightAuthoritys,
      accountCancel,
      register,
      vip,
      addGold,
      netmap,
      applysuggest,
      shrinkage,
      noRealAccount,
      entry_v2
    },
    data() {
      return {
        isPrint: false,
        loadingModal: false,
        loadingSpin: false, //页面加载中
        showSave: true, //是否显示报错按钮
        tabs: {},
        showmd: true,
        filesList: [], //附件模板
        draftsId: "", //草稿id
        reqId: "", //工单ID
        applyPage: "",
        submintLoading: false,
        showSubmint: false,
        applyName: "",
        menuid: "",
        showApplyId: "",
        serviceType: "",
        childrenType: "",
        demandUsers: [],
        submintInfo: { //提交申请的参数
          remark: {
            isDraft: "0",
            draftID: "",
            isReStart: "0",
            restartReqId: "",
            restartTaskId: "",
          },
          reqSubject: "",
          startTime: "",
          hopeTime: "",
          reqDes: "",
          content: "",
          nextOperator: {
            id: "",
            userName: "",
          }
        },
        content: {}
      }
    },
    created: function() {
      this.tabs = this.$store.state.tabs
      console.log("params----->>", this.params)
      this.serviceType = this.params.serviceType + ''
      this.childrenType = this.params.taskDefKey
      this.draftsId = this.params.draftsId
      if (this.params.reqId === undefined || this.params.reqId === null) {
        this.reqId = null
      } else {
        this.reqId = this.params.reqId
      }

      if (this.tabs.list[this.$store.state.tabs.list.length - 1].tabKey == this.tabs.tabsPage) {
        console.log('首次进入')
      } else {
        console.log('刷新进入')
        this.showmd = false
      }

      this.setApplyPage()
      this.getFirstFiles()
    },
    methods: {
      //获取附件模板
      getFirstFiles() {
        var url = this.CONFIG.base_url_test + "/first-file/" + this.serviceType
        var that = this;
        ajax.get({
          url: url,
          that: that,
          success: function(data) {
            that.filesList = data
          }
        })
      },
      //设置需要显示的申请页面
      setApplyPage() {
        console.log("====this.serviceType======", this.serviceType);
        if (this.params.taskId != undefined || this.params.taskId != null) {
          this.showSave = false
        }
        this.applyName = this.params.depname
        // this.applyPage = 'entry_test'
        this.applyPage = this.serviceType
        if (this.serviceType === 'resourceAllo') {
          this.applyPage = "resourceAllo"
        } else if (this.serviceType === 'accountNoBigdata' || this.serviceType === 'accountBigdata') {
          this.applyPage = "accountApply"
        } else if (this.serviceType === 'sonAccountAllo' || this.serviceType === 'sonAccountCancel') {
          this.applyPage = 'sonAccountAllo'
        } else if (this.params.taskId != undefined && this.params.taskId != null) { //发起子工单
          this.applyName = "发起子工单 / " + this.params.depname
          this.applyPage = "children"
        } else if (this.serviceType === 'netmapping') {
          this.applyPage = 'netmap'
        } else if (this.serviceType === 'opinion') {
          this.applyPage = 'applysuggest'
        }

      },
      showSubmintPage() {
        if (this.$refs.apply.validate()) {
          this.loadingModal = true
          this.content = this.$refs.apply.getInfo()
          console.log("content------>", JSON.stringify(this.content))
          this.getFirstHandle()
          this.showSubmint = true
        }
      },
      submint() {
        console.log('submintInfo---->', this.submintInfo)
        this.submintLoading = true
        this.utils.delay(() => {
          if (this.demandUsers.length > 0 && this.submintInfo.nextOperator.id === "") {
            this.$Message.error("请选择下一步处理人")
            this.$nextTick(() => {
              this.submintLoading = false
            })
          } else {
            var temp = {
              reqSubject: "",
              startTime: "",
              hopeTime: "",
              reqDes: "",
            }
            var url = ""
            var content = this.content
            if (this.params.taskId != undefined && this.params.taskId != null) { //发起子工单
              url = this.CONFIG.base_url_test + "/start-son/" + this.serviceType
              temp = {
                reqId: this.params.reqId,
                taskId: this.params.taskId,
                processInsId: this.params.processInsId,
                nextOperator: this.submintInfo.nextOperator,
                content: content
              }
            } else {
              console.log('submit-content:', content)
              this.submintInfo.content = content
              temp = this.submintInfo
              url = this.CONFIG.base_url_test + "/start/" + this.serviceType
              if (this.draftsId != undefined && this.draftsId != null && this.draftsId != '') {
                temp.remark.isDraft = 1
                temp.remark.draftID = this.draftsId
              } else if (this.params.reqId != null && this.params.reqId != '') {
                temp.ramark.isReStart = 1
                temp.remark.restartReqId = this.params.reqId
                temp.remark.restartTaskId = this.params.restartTaskId
              }
              if (content.demandAssign != undefined) {
                temp.remark.demandAssign = content.demandAssign
              } //安全申请页面特有字段
              if (this.serviceType === 'severMachine') {
                temp.projectId = content.object.projectInfo.projectId
              }
              if (this.serviceType === 'netmapping') {
                temp.projectId = content.base.projectId
              }
            }
            if (this.childrenType != undefined && this.childrenType != null) {
              temp.taskDefKey = this.childrenType
            }
            if (content.puslicInfo != undefined) {
              temp.reqSubject = content.puslicInfo.reqSubject
              temp.startTime = parseInt(content.puslicInfo.startTime)
              temp.hopeTime = parseInt(content.puslicInfo.hopeTime)
              temp.reqDes = content.puslicInfo.reqDes
            }
            if (content.base != undefined) {
              temp.dptName = content.base.dptName
            }
            if (this.serviceType === 'hightAuthoritys') {
              temp.projectId = content.object.projectId
              temp.roleId = content.object.roleId
            }
            if (this.serviceType === 'opinion') {
              temp.projectId = content.projectId
              temp.roleId = content.roleId
            }

            if (this.serviceType === 'loadBalance') {
              temp.managerId = content.object.pmId
            }
            if (this.serviceType === 'securityCheck') {
              temp.managerId = content.object.chId
            }

            if (this.serviceType === 'accountBigdata' || this.serviceType === 'accountNoBigdata') {
              temp.roleId = content.object.roleId;
              temp.projectId = content.object.projectId;
            }

            if (this.serviceType === 'vip') {
              temp.roleId = content.roleId;
              temp.projectId = content.projectId;
              temp.reqSubject = content.reqSubject;
            }

            if (this.serviceType === 'addGold') {
              temp.roleId = content.roleId;
              temp.projectId = content.projectId;
              temp.reqSubject = content.reqSubject;
              temp.reqDes = content.reqDes;
            }

            if (this.serviceType === 'shrinkage') {
              temp.roleId = content.object.roleId;
              temp.projectId = content.object.projectId;
              temp.reqSubject = content.object.reqSubject;
            }

            if (this.serviceType === 'noRealAccount') {
              temp.roleId = content.roleId;
              temp.projectId = content.projectId;
              temp.reqSubject = content.reqSubject;
              temp.reqDes = content.reqDes;
              temp.des = content.des;
            }
            if (this.serviceType === 'accountCancel') {
              temp.projectId = content.object.projectId;
            }

            if (this.serviceType === 'net') {
              temp.roleId = content.base.roleId;
              temp.projectId = content.base.projectId;
            }
            if (this.serviceType === 'entry' || this.serviceType === 'entry_v2') {
              temp.dptName = content.object.base.dptName
              temp.roleId = content.base.roleId;
              temp.projectId = content.base.projectId;
            }
            if (this.applyPage === 'children' || this.serviceType === 'resourceAllo') {
              temp.projectId = this.params.projectId;
            }
            console.log("submint------>", JSON.stringify(temp))
            var that = this;
            ajax.post({
              url: url,
              that: that,
              data: temp,
              success: function(data) {

                that.$Message.success('提交成功!');
                // setTimeout(() => {

                var index = -1;
                that.tabs.list.some((item, i) => {
                  if (item.tabKey === that.tabs.tabsPage) {
                    index = i
                    return true
                  }
                })
                var isLast = false //判断是否操作上一步页面是否还存在
                that.tabs.list.some((item, i) => {
                  if (item.tabKey === that.tabs.lastPage) {
                    isLast = true
                    return true
                  }
                })
                that.tabs.isEdit = ""
                if (isLast && that.tabs.lastPage != "") {
                  that.tabs.tabsPage = that.tabs.lastPage
                  that.tabs.lastPage = ""
                } else {
                  that.tabs.tabsPage = 'processmain'
                }

                that.tabs.list.splice(index, 1)
                that.submintLoading = false
                console.log(that.tabs)
                that.$store.commit("tabs", that.tabs);
                // }, 1000);
              },
              err: function(data) {
                that.submintLoading = false
              }
            })
          }
        }, 1000)


      },
      setNextOperator(value) {
        this.demandUsers.some(item => {
          if (item.id == value) {
            this.submintInfo.nextOperator = {
              id: value,
              userName: item.user_name
            }
            return true
          }
        });
      },
      saveDraft() {
        this.loadingSpin = true
        this.utils.delay(() => {
          console.log("this.draftsId ----> ", this.draftsId)
          var url = this.CONFIG.base_url_test + "/draft/" + this.serviceType
          var temp = this.$refs.apply.getInfo()
          console.log("temp ---->", temp)
          if (this.draftsId != undefined && this.draftsId != null && this.draftsId != '') {
            url = this.CONFIG.base_url_test + "/draft/" + this.draftsId
            var that = this;
            ajax.put({
              url: url,
              that: that,
              data: temp,
              success: function(data) {
                console.log("save-------------->", data)
                that.$Message.success('保存成功!');
                that.loadingSpin = false
              }
            })
          } else {
            var that = this;
            ajax.post({
              url: url,
              that: that,
              data: temp,
              success: function(data) {
                console.log("save-------------->", data)
                that.draftsId = data
                that.$Message.success('保存成功!');
                that.loadingSpin = false
              }
            })
          }
        }, 2000)
      },
      //获取第一步处理人
      getFirstHandle() {
        console.log('获取第一步处理人', this.content)
        var paramsInfo = this.$refs.apply.getHandleUserParams("")
        if (this.params.reqId === undefined || this.params.reqId === null) {
          paramsInfo.reqId = ""
        } else {
          paramsInfo.reqId = this.params.reqId
        }

        console.log(paramsInfo)
        var url = this.CONFIG.base_url_test + "/first-can/" + this.serviceType
        var that = this;
        ajax.get({
          url: url,
          that: that,
          params: paramsInfo,
          success: function(data) {
            // that.getUserInfoById(data)
            if (data.userList != null) {
              if (data.userList.length == 1) {
                that.submintInfo.nextOperator = {
                  id: data.userList[0].id,
                  userName: data.userList[0].user_name
                }
              }
              that.demandUsers = data.userList
            }
            that.loadingModal = false
          }
        })
      },
    },
    props: {
      params: {}
    }
  }
</script>
